#ifndef HEAD_H
#define HEAD_H
#include<iostream>
using namespace std;

struct node{
    node* nx;
    int data;
};

class arraylist{
private:
    node*head;
    node*tail;
    node*current;
    int length=0;
    int r[2];
public:
    arraylist();
    void length_input();
    void creatlist(int i);
    void showlist();
    ~arraylist();

    int getmin();
    node* findnode(int);
    node* nodedelete(node*todelete);

    void x2_01();
    void x2_02();
    void x2_03();

    void inputrange();
};


arraylist::arraylist(){
    head=tail=current=NULL;
    length_input();
}

void arraylist::length_input(){
    cout<<"please input the length of arraylist:";
    cin>>length;
        if(length){
            creatlist(length);
        }
        else{
            length_input();
        }
}


void arraylist::creatlist(int i){
    int counter=0;

    head=tail=new node;
    current=head;

    while(counter<i){
        tail=current=current->nx=new node;

        cout<<"\ninput the no."<<++counter<<" number: ";
        cin>>current->data;
    }

    tail->nx=NULL;
//    delete current;别在这贱兮兮的干这活！去掉了这个你试试！
}


arraylist::~arraylist(){
    while(head!=NULL){
        current=head;
        head=head->nx;
        delete current;
    }
    head=tail=NULL;
}


void arraylist::showlist(){
    node*p=head->nx;
    cout<<endl;
    int counter=0;

    while(p!=NULL){
        cout<<p->data<<"\t";
        p=p->nx;
        counter++;
        if(counter>100)
            break;
    }
    cout<<endl;
}

int arraylist::getmin(){
    current=head->nx;

    int min=current->data;
    while(current->nx!=NULL){
        current=current->nx;
        if(current->data<min)
            min=current->data;
    }
    return min;
}

//查找结点
node*arraylist::findnode(int n){
    current=head->nx;
    bool flag=0;
    while(current!=NULL){
        if(current->data==n){
            flag=1;
            return current;
        }
        current=current->nx;
    }
    if(!flag){
        return NULL;
        cout<<"\nno such node"<<endl;
    }
}

//删除要删除的节点，返回已经删除的节点的前一个节点的指针，生成新链表。
node*arraylist::nodedelete(node * todelete){
    if(todelete!=NULL)
    {
        node* q=head;
        while(q->nx!=todelete){
            q=q->nx;
        }
        node*p=q;
        q->nx=todelete->nx;
        delete todelete;
        showlist();
        return p;
    }
}


void arraylist::x2_01(){
   int min=getmin();
   cout<<"min="<<min;

   node* p=nodedelete(findnode(min));
   node*q;
//   node* q=p->nx;
   node* pretail=head;
   node* curtail;
   while(pretail->nx!=tail){
       pretail=pretail->nx;
   }
   if(pretail!=p)
   {
       curtail=tail;
       tail=pretail;
       tail->nx=NULL;

       q=p->nx;;
       p->nx=curtail;
       curtail->nx=q;
   }
//   delete q;

   cout<<"\n02_(01):";
   showlist();
}


void arraylist::x2_02(){
    int number;
    cout<<"input the number to delete：";
    cin>>number;
    current=head;
    node*flag=current;
    node*p;
    while(flag!=tail){
        p=findnode(number);
        if(p==NULL){
            flag=tail;
        }
        else{
            nodedelete(p);
        }
    }
    cout<<"02_(02):";
    showlist();
}

void arraylist::x2_03(){
    inputrange();
    node*p=head;

    while(p->nx!=NULL){
        p=p->nx;
        if(p->data>=r[0]&&p->data<=r[1]){
            p=nodedelete(p);
        }
    }
}

void arraylist::inputrange(){
    int s,t;
    cout<<"delete the nodes which holds the number within range [s,t]"<<endl
       <<"input s:";
    cin>>s;
    cout<<"\ninput t:";
    cin>>t;
    if(s<=t){
        r[0]=s;
        r[1]=t;
    }
    else{
        cout<<"\nplease input the range again.\n";
        inputrange();
    }
}


#endif // HEAD_H
